function tobacco
    clc
    close all
    fclose all
    fileName = 'F:\玉溪烟草\seed\seed_55.json';
    fileName = 'F:\玉溪烟草\seed\seed (1).json';
    
    fileName = 'F:\玉溪烟草\2022-04-28新微信图片\20221021漏标注\上次未标注\95w.json'
    
    
% %     fileName = 'F:\玉溪烟草\seed\seed (4).json';
%     fileName = 'F:\玉溪烟草\matlab\expand\seed (4)_4__1.json';
    
% % %     json_src = JsonObject(fileName);%%元数据
% % %     figure
% % %     json_src.showImage(true);
% %     return;
% % % % % % %     DoTestStep4()
% % % % % % %     return;
    
% %     fileName = 'F:\玉溪烟草\matlab\aaa.json';
%     fileName = 'F:\玉溪烟草\matlab\seed_55-1.json';
% %     fileName = '‪seed_55.json';
%     fileName = 'seed_55.json'    
    template_file =  'F:\玉溪烟草\matlab\template.json';

    [dirpath, imgName,~] = fileparts(fileName);

    imgPath = [dirpath, '\', imgName, '.jpg']
    obj = CImage(imgPath);
    obj.showGrids();
    
    json_src = JsonObject(fileName, true);
    json_temp = JsonObject(template_file, false);
    json_temp.json_data.shapes = json_temp.json_data.shapes;
    
    img = obj.img;
    fileSave = [dirpath, '\', imgName, '22.jpg']
    isLowHigh = false
    json_temp.encodeImageData(img, fileName, isLowHigh)
    
    return
    
    
% %     boxes = obj.getPatchGrid();
    obj.showGrids();
%     return;
    
    reOrder = true;
    json_src = JsonObject(fileName, reOrder);%%元数据
    
    figure
    json_src.showImage(true);
    
    json_temp = JsonObject(template_file);
    img = json_src.getImage();
% %     args.savejson_dir = 'F:\玉溪烟草\matlab\save';
% %     args.saveimg_dir = 'F:\玉溪烟草\matlab\save';
    args.save_seg_json_dir = 'F:\玉溪烟草\matlab\seg';
    args.save_seg_img_dir = 'F:\玉溪烟草\matlab\seg';
    args.fileName = fileName;
    obj.segImagePieces(json_src, json_temp, img, args)
    return;


% % % %     json_obj = ImageObject(fileName)
% % % % 
% % % %     [dirpath, imgName,~] = fileparts(fileName);
% % % % 
% % % %     imgPath = [dirpath, '\', json_obj.imagePath]
% % % %     img = imread(imgPath);    
% % % %     image(img)
% % % %     axis equal
% % % %    
% % % %     drawBoxes(json_obj)
%     return
    
    savejson_dir = 'F:\玉溪烟草\matlab\样本扩增\';
    saveimg_dir = 'F:\玉溪烟草\matlab\样本扩增\';
    json_obj = JsonObject(fileName);
    json_obj.showImage(true)
    
    
    return;

    json_new = copy(json_obj);
    img2 = img;
    imgName2 = imgName;
    for i = 1 : 3
        imgName = [imgName, '-r'];
        json_save = [savejson_dir, imgName, '.json'];
        img_save  = [saveimg_dir, imgName, '.jpg'];
        
        img = json_new.rotateImage90(img);
        json_new.encodeImageData(img, img_save, false)
        json_new.saveJsonFile(json_save);   
    end
    
    json_new = copy(json_obj);
    img = img2;
    imgName = imgName2;
    
    
    imgName = [imgName, '-f'];
    json_save = [savejson_dir, imgName, '.json'];
    img_save  = [saveimg_dir, imgName, '.jpg'];

    img = json_new.fliplrImage(img);
    json_new.encodeImageData(img, img_save, false)
    json_new.saveJsonFile(json_save);
    
    for i = 1 : 3
        imgName = [imgName, '-r'];
        json_save = [savejson_dir, imgName, '.json'];
        img_save  = [saveimg_dir, imgName, '.jpg'];

        img = json_new.rotateImage90(img);
        json_new.encodeImageData(img, img_save, false)
        json_new.saveJsonFile(json_save);    
    end  

    disp('done')
    return;


% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 
% % % % %     json_new = JsonObject(fileName);
% % % % %     json_save = ['F:\玉溪烟草\matlab\', imgName, 'f.json'];
% % % % %     img_save  = ['F:\玉溪烟草\matlab\', imgName, 'f.png'];
% % % % %     json_new.fliplrImage();
% % % % %     json_new.saveJsonFile(json_save);
% % % % %     imgNew = fliplr(img);
% % % % %     image(imgNew)
% % % % %     axis equal
% % % % %     imwrite(imgNew, img_save);
% % % % %     return
    json_new = JsonObject(fileName);
    json_save = ['F:\玉溪烟草\matlab\', imgName, '-1.json'];
    img_save  = ['F:\玉溪烟草\matlab\', imgName, '-1.jpg'];
    imgNew = json_new.rotateImage90(img);
    json_new.encodeImageData(imgNew, img_save, false)
    json_new.saveJsonFile(json_save);
    
    
    image(imgNew)
    axis equal
    imwrite(imgNew, img_save);
    return    
    
    
    
% %     base64 = org.apache.commons.codec.binary.Base64
% %     res = base64.decode(json2.imageData);
    
% %     res = matlab.net.base64encode(img);
% %     unicodestr = native2unicode(img)
% %     res = base64.encode(unicodestr)
    
    
% %     return
    

    % % % % % % 逆时针旋转90
    [json_new, imgNew] = rotateImage90(img, json_obj);
    figure
    image(imgNew)
    axis equal
    drawBoxes(json_new)
% %     json_new.boxes    
    return;
    
    % % % % % % 左右互换
    [json_new, imgNew] = fliplrImage(img, json_obj);
    figure
    image(imgNew)
    axis equal
    drawBoxes(json_new)
    return;
    

end
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 
function DoTestStep4()
    datapath = 'F:\玉溪烟草\seed\';%2022华农反馈的图片
    args.bShow = false;
    args.datapath = datapath;
    args.savejson_dir = 'F:\玉溪烟草\matlab\样本扩增\';
    args.saveimg_dir = 'F:\玉溪烟草\matlab\样本扩增\';
    args.save_seg_json_dir = 'F:\玉溪烟草\matlab\seg\';
    args.save_seg_img_dir = 'F:\玉溪烟草\matlab\seg\';
    args.template_file =  'F:\玉溪烟草\matlab\template.json';
    
    args.save_expand_json_dir = 'F:\玉溪烟草\matlab\expand\';
    args.save_expand_img_dir  = 'F:\玉溪烟草\matlab\expand\';   
    args.save_no_json_dir     = 'F:\玉溪烟草\matlab\expand\no\';
    args.save_no_img_dir      = 'F:\玉溪烟草\matlab\expand\no\';
    
    datapath = args.save_seg_json_dir

    bOrder = true;
    savejson_dir = args.save_expand_json_dir;
    saveimg_dir  = args.save_expand_img_dir; 
    saveno_dir = args.save_no_json_dir;
    
    jsonName = 'F:\玉溪烟草\matlab\seg\seed (4)_4.json';
    disp(jsonName);           

    [dirpath, imgName, ext] = fileparts(jsonName);                    

    json_obj = JsonObject(jsonName, false);%%这些文件都已经排好序坐标  
    imgPath = fullfile(dirpath, json_obj.get_imagePath()); 

    if json_obj.hasShapes() == false
        disp(['   no shapes:', num2str(i)]);
        copyfile(jsonName, saveno_dir);   
        copyfile(imgPath, saveno_dir);
        return;
    end

    jsonNameDst = fullfile(savejson_dir, [imgName, '__0.json']);
    imgNameDst  = fullfile(saveimg_dir, [imgName, '__0.jpg']);

    json_obj.json_data.imagePath = [imgName, '__0'];

    copyfile(jsonName, jsonNameDst);   
    copyfile(imgPath, imgNameDst);

    img = imread(imgPath);

    no = 1;
    SaveThreeRotation(json_obj, img, imgName, savejson_dir, saveimg_dir, no);
    no = 4;
    SaveOneFlipAndThreeR(json_obj, img, imgName, savejson_dir, saveimg_dir, no);
    disp('done');
end
%% 3次旋转
function SaveThreeRotation(json_obj, img, imgName, savejson_dir, saveimg_dir, no)
    json_new = copy(json_obj);    
    
    for i = 1 : 3
% %         imgName = [imgName, '-r'];   here
        imgName2 = [imgName, '__', num2str(no)];
        no = no + 1;        
        
        json_save = [savejson_dir, imgName2, '.json'];
        img_save  = [saveimg_dir, imgName2, '.jpg'];

        img = json_new.rotateImage90(img);
        json_new.encodeImageData(img, img_save, true)
        json_new.saveJsonFile(json_save);   
    end
end
% 1次翻转
function SaveOneFlipAndThreeR(json_obj, img, imgName, savejson_dir, saveimg_dir, no)
    json_new = copy(json_obj);
    
% %     imgName = [imgName, '-f'];
    imgName2 = [imgName, '__', num2str(no)];
    no = no + 1;
    
    json_save = [savejson_dir, imgName2, '.json'];
    img_save  = [saveimg_dir, imgName2, '.jpg'];

    img = json_new.fliplrImage(img);
    json_new.encodeImageData(img, img_save, false)
    json_new.saveJsonFile(json_save);
    
    SaveThreeRotation(json_new, img, imgName, savejson_dir, saveimg_dir, no);
end

% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
%%旋转文件90度
function [json_new, imgNew] = rotateImage90(img, json_obj)
    imgNew = rot90(img);
    json_new = copy(json_obj);
% %     json_obj.imagePath = 'aaaaaaaaaa';
    
    boxes = json_obj.boxes;
    
    imageWidth = json_obj.imageWidth;
    for i = 1 : length(boxes)
        box = boxes(i, :);
        xa = box(1);
        ya = box(2);
        xc = box(3);
        yc = box(4);
        ww = box(5);
        hh = box(6);
        xd = xc;
        yd = ya;
        xb = xa;
        yb = yc;
        
        boxes(i, :) = [yd, imageWidth- 1 - xd, yb, imageWidth- 1 - xb, hh, ww];
    end
    json_new.boxes = boxes;
    json_new.imageHeight = json_obj.imageWidth;
    json_new.imageWidth = json_obj.imageHeight;   
end

function [json_new, imgNew] = fliplrImage(img, json_obj)
    imgNew = fliplr(img);
    json_new = copy(json_obj);
    
    boxes = json_obj.boxes;
    
    imageWidth = json_obj.imageWidth;
    for i = 1 : length(boxes)
        box = boxes(i, :);
        xa = box(1);
        ya = box(2);
        xc = box(3);
        yc = box(4);
        ww = box(5);
        hh = box(6);
        xd = xc;
        yd = ya;
        xb = xa;
        yb = yc;
        
        boxes(i, :) = [imageWidth-1 - xd, yd, imageWidth-1 - xb, yb, ww, hh];
    end
    json_new.boxes = boxes;

end

%%
function drawBoxes(json_obj)
	hold on
    boxes = json_obj.boxes;
    labels = json_obj.labels;
    for i = 1 : size(boxes,1)
        box = boxes(i, :);
        label = labels{i};

        if strcmp(label, 'seed') == true
            rectangle('Position',[box(1)+1, box(2)+1, box(5), box(6)], 'EdgeColor','b')
        else
            rectangle('Position',[box(1)+1, box(2)+1, box(5), box(6)], 'EdgeColor','r')
        end
    end        
    hold off
end